package cn.tradewin.advisor.persist.services.impl;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.slf4j.Logger;

import cn.tradewin.advisor.persist.mapper.CalendarMapper;
import cn.tradewin.advisor.persist.model.Advcalendar;
import cn.tradewin.advisor.persist.model.CalendarCategory;
import cn.tradewin.advisor.persist.model.CalendarDates;
import cn.tradewin.advisor.persist.model.CodeLabel;
import cn.tradewin.advisor.persist.services.CalendarService;

public class CalendarServiceImpl implements CalendarService {

	private final SqlSessionFactory sqlSessionFactory;
	
	private final Logger log;
	
	public CalendarServiceImpl(SqlSessionFactory sqlSessionFactory, Logger log) {
		this.sqlSessionFactory = sqlSessionFactory;
		this.log = log;
	}
	
	@Override
	public int addCalendar(Advcalendar calendar) {
		SqlSession session = null;
		try {
			session = sqlSessionFactory.openSession();
			CalendarMapper mapper = session.getMapper(CalendarMapper.class);
			int count = mapper.addCalendar(calendar);
			session.commit();
			return count;
		} catch (Exception e) {
			if (session != null) {
				session.rollback();
			}
			throw new RuntimeException(e.getMessage());
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}
	
	@Override
	public void addCalendarWithWeekend(Advcalendar calendar,
			List<CalendarDates> dates) {
		SqlSession session = null;
		try {
			session = sqlSessionFactory.openSession();
			CalendarMapper mapper = session.getMapper(CalendarMapper.class);
			mapper.addCalendar(calendar);
			for (CalendarDates d : dates) {
				mapper.insertCalendarDate(d);
			}
			session.commit();
		} catch (Exception e) {
			if (session != null) {
				session.rollback();
			}
			throw new RuntimeException(e.getMessage());
		} finally {
			if (session != null) {
				session.close();
			}
		}
		
	}
	
	@Override
	public Advcalendar getCalendar(String id) {
		SqlSession session = null;
		try {
			session = sqlSessionFactory.openSession();
			CalendarMapper mapper = session.getMapper(CalendarMapper.class);
			return mapper.getCalendar(id);
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage());
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

	
	@Override
	public List<Advcalendar> getCalendars() {
		SqlSession session = null;
		try {
			session = sqlSessionFactory.openSession();
			CalendarMapper mapper = session.getMapper(CalendarMapper.class);
			return mapper.getCalendars();
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage());
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

	@Override
	public List<CodeLabel> getCalendarCategory(String locale) {
		SqlSession session = null;
		try {
			session = sqlSessionFactory.openSession();
			CalendarMapper mapper = session.getMapper(CalendarMapper.class);
			return mapper.getCalendarCategory(locale);
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage());
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

	@Override
	public List<CalendarCategory> getCalendarAllCategory() {
		SqlSession session = null;
		try {
			session = sqlSessionFactory.openSession();
			CalendarMapper mapper = session.getMapper(CalendarMapper.class);
			return mapper.getCalendarAllCategory();
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage());
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}
	
	@Override
	public List<CalendarDates> getCalendarDates(String calendarId) {
		SqlSession session = null;
		try {
			session = sqlSessionFactory.openSession();
			CalendarMapper mapper = session.getMapper(CalendarMapper.class);
			return mapper.getCalendarDates(calendarId);
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage());
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}


	@Override
	public int addCalendarDates(List<CalendarDates> dates) {
		SqlSession session = null;
		try {
			int count = 0;
			session = sqlSessionFactory.openSession();
			CalendarMapper mapper = session.getMapper(CalendarMapper.class);
			for (CalendarDates date:dates) {
				count += mapper.insertCalendarDate(date);
			}
			session.commit();
			return count;
		} catch (Exception e) {
			if (session != null) {
				session.rollback();
			}
			throw new RuntimeException(e.getMessage());
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

	@Override
	public int addCalendarCategory(CalendarCategory category) {
		SqlSession session = null;
		try {
			session = sqlSessionFactory.openSession();
			CalendarMapper mapper = session.getMapper(CalendarMapper.class);
			int count = mapper.addCalendarCategory(category);
			session.commit();
			return count;
		} catch (Exception e) {
			if (session != null) {
				session.rollback();
			}
			throw new RuntimeException(e.getMessage());
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

	@Override
	public int delCalendarCategory(Byte id) {
		SqlSession session = null;
		try {
			session = sqlSessionFactory.openSession();
			CalendarMapper mapper = session.getMapper(CalendarMapper.class);
			int count = mapper.delCalendarCategory(id);
			session.commit();
			return count;
		} catch (Exception e) {
			if (session != null) {
				session.rollback();
			}
			throw new RuntimeException(e.getMessage());
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

	@Override
	public int delCalendarDates(Map<String, Object> param) {
		SqlSession session = null;
		try {
			session = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
			CalendarMapper mapper = session.getMapper(CalendarMapper.class);
			int count = mapper.delCalendarDates(param);
			session.commit();
			return count;
		} catch (Exception e) {
			if (session != null) {
				session.rollback();
			}
			throw new RuntimeException(e.getMessage());
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

	@Override
	public int delCalendarDate(String calendarId, String date) {
		SqlSession session = null;
		try {
			session = sqlSessionFactory.openSession();
			CalendarMapper mapper = session.getMapper(CalendarMapper.class);
			int count = mapper.delCalendarDate(calendarId, date);
			session.commit();
			return count;
		} catch (Exception e) {
			if (session != null) {
				session.rollback();
			}
			throw new RuntimeException(e.getMessage());
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}
}